package ovm.gui.views;

import com.vaadin.ui.VerticalLayout;
import ovm.gui.forms.AbstractEntityForm;
import ovm.management.EntityCollectionManager;
import ovm.management.EntityCollectionProperty;

import java.util.List;

public class AbstractEntityView extends AbstractView {

    protected VerticalLayout layout;
    protected AbstractEntityForm form;
    
    protected Object entity;
    protected Class entityClass;
    
    protected Boolean editable;

    public AbstractEntityView (AbstractEntityForm form, Boolean editable) {
        super();

        this.form = form;
        this.editable = editable;
        this.entity = form.getEntity();
        this.entityClass = form.getEntityClass();

        layout = new VerticalLayout();
        layout.addComponent(this.form);

        List<EntityCollectionProperty> collectionPropertyList = EntityCollectionManager.getCollections(entityClass);
        
        for (EntityCollectionProperty property : collectionPropertyList) {
            Class referenceClass = property.referenceClass;
            String referenceParameter = property.referenceParameter;

            AbstractEntityListView additionalView = EntityListViewFactory.createEntityListView(referenceClass, false);
            additionalView.setFilter(referenceParameter, entity);

            layout.addComponent(additionalView);
        }

        addComponent(layout);
    }

}
